package com.tutos.ui.login;

import java.awt.Label;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.transform.stream.StreamSource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;

import com.google.inject.Key;
import com.tutos.GuiceServletInjector;
import com.tutos.ui.BaseMain;
import com.tutos.ui.TutosApplication;
import com.tutos.ui.monitor.MonitorAnnotation;
import com.tutos.ui.student.StudentAnnotation;
import com.vaadin.terminal.ClassResource;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.StreamResource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.LoginForm.LoginEvent;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;

public class LoginScreen extends VerticalLayout
{
	private static final long serialVersionUID = 1L;
	private TutosApplication app;

	public LoginScreen(TutosApplication app)
	{
		this.app = app;

		setSizeFull();
		setStyleName("login");

		Panel loginPanel = new Panel("Inicio");
		loginPanel.setWidth("340px");

		LoginForm loginForm = new LoginForm();
		loginForm.setPasswordCaption("Contraseña");
		loginForm.setUsernameCaption("Correo");
		loginForm.setLoginButtonCaption("Entrar");

		loginForm.setHeight("110px");
		loginForm.addListener(new MyLoginListener(this.app, loginForm));
		
		Button crear = new Button("Crear cuenta");
		Resource img = new ThemeResource("perfil.gif");
		System.out.println("Salio?");
		Embedded image = new Embedded("", img);
		
		image.setHeight("85px");
		HorizontalLayout organizador = new HorizontalLayout();
		VerticalLayout org2 = new VerticalLayout();
		//org2.setSizeFull();
		org2.addComponent(image);
		org2.setComponentAlignment(image, Alignment.TOP_CENTER);
		org2.addComponent(crear);
		org2.setComponentAlignment(image, Alignment.MIDDLE_CENTER);
		
		organizador.addComponent(loginForm);
		organizador.addComponent(org2);
		
		RegistroEstudiante crearCuenta = new RegistroEstudiante();
	
		
		//	crearCuenta.setWidth("340px");
		
		loginPanel.addComponent(organizador);
		
		final Window ventanaCrearCuenta = new Window("Crear cuenta");
		
		addComponent(loginPanel);
		setComponentAlignment(loginPanel, Alignment.TOP_RIGHT);
		
		ventanaCrearCuenta.setSizeUndefined();
		ventanaCrearCuenta.addComponent(crearCuenta);
		ventanaCrearCuenta.setWidth("420px");
		ventanaCrearCuenta.setHeight("-1px");
		
		ventanaCrearCuenta.setModal(true);
		ventanaCrearCuenta.setResizable(false);
		
		crear.addListener(new ClickListener() {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				getApplication().getMainWindow().addWindow(ventanaCrearCuenta);
				ventanaCrearCuenta.setVisible(true);
			}
		});

	}

	private static class MyLoginListener implements LoginForm.LoginListener
	{
		private static final long serialVersionUID = 1L;
		private TutosApplication app;
		private LoginForm loginForm;


		public MyLoginListener(TutosApplication app, LoginForm loginForm)
		{
			this.app = app;
			this.loginForm = loginForm;
		}


		@Override
		public void onLogin(LoginEvent event)
		{
			String username = event.getLoginParameter("username");
			String password = event.getLoginParameter("password");

			try
			{
				TutosApplication.getInstance().login(username, password);

				BaseMain baseMain;
				
				Subject subject = SecurityUtils.getSubject();
				
				if(subject.hasRole("monitor")){
					baseMain = GuiceServletInjector.injector.getInstance(Key.get(BaseMain.class,MonitorAnnotation.class));
				}else{
					baseMain = GuiceServletInjector.injector.getInstance(Key.get(BaseMain.class,StudentAnnotation.class));
				}
				
				app.getMainWindow().removeAllComponents();
				app.getMainWindow().addComponent(baseMain.getComponent());
			}
			catch (UnknownAccountException uae)
			{
				this.loginForm.getWindow().showNotification("Invalid User", Notification.TYPE_ERROR_MESSAGE);
			}
			catch (IncorrectCredentialsException ice)
			{
				this.loginForm.getWindow().showNotification("Invalid User", Notification.TYPE_ERROR_MESSAGE);
			}
			catch (LockedAccountException lae)
			{
				this.loginForm.getWindow().showNotification("Invalid User", Notification.TYPE_ERROR_MESSAGE);
			}
			catch (ExcessiveAttemptsException eae)
			{
				this.loginForm.getWindow().showNotification("Invalid User", Notification.TYPE_ERROR_MESSAGE);
			}
			catch (AuthenticationException ae)
			{
				this.loginForm.getWindow().showNotification("Invalid User", Notification.TYPE_ERROR_MESSAGE);
			}
			catch (Exception ex)
			{
				this.loginForm.getWindow().showNotification("Exception " + ex.getMessage(), Notification.TYPE_ERROR_MESSAGE);
			}
		}
	}
}
